﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
namespace dohoa2
{
    public partial class Form1 : Form
    {
        Bitmap anh;
        Graphics g;
        Pen vietchinh, vietchinh2, vietphu;
        SolidBrush cayco;
        int chedo=1, chedobiendoi=0,dangchon=0, lan=0, landt=0;
        Chinhve[] vatthe; int soluong = 0;
        
        int  lanbamchuot = 0;
        Point diem1, diem2, diem11;
        Color maunentoancuc,mauvetoancuc;
     //   Cline duong;
        //Cduongtron dtr;
      //  Chinhchunhat hcn;
        Chinhve hv;
        bool co = false;
        int i;
        public Form1()
        {
            InitializeComponent();
            g = pic1.CreateGraphics();
            vietchinh = new Pen(Color.Yellow);
            vietchinh2 = new Pen(Color.Blue);
            vietphu = new Pen(Color.Green);
            anh = new Bitmap(pic1.Width, pic1.Height, g);
            cayco = new SolidBrush(Color.DarkViolet);
            vatthe = new Chinhve [100];
            maunentoancuc = pic1.BackColor;
            mauvetoancuc = Color.Green ;
            diem1 = new Point(); diem2 = new Point();
        }

        private void Vepic(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                if (co == false)
                {
                    switch (chedo)
                    {
                        case 0: xulychon(e); break;
                        case 1: case 2: case 3: case 5:
                            xulychuotclickkhikhongvedagiac(e); break;
                         
                     //   case 1: xulychuotclickkhiveduongthang(e); break;

                      //  case 2: xulychuotclickkhiveduongtron(e); break;
                      //  case 3: xulychuotclickkhivehinhchunhat(e); break;
                        case 4: xulychuotclickkhivedagiac(e); break;
                      //  case 5: xulychuotclickkhiveparabol(e); break; 
                    }

                }
            }
            else
            {
                hienthuoctinh(e); 
              
            }
        }
        private int round(float a)
        {
            return (int)(a + 0.5);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            g.Clear(pic1.BackColor);
            int j;
            Color mau = pic1.BackColor;

            for (int i = 0; i < anh.Width; i++)
                for (j = 0; j < anh.Height; j++)
                    anh.SetPixel(i,j,mau);
            soluong = 0;
        }

        private void xulychuotclickkhikhongvedagiac(MouseEventArgs e)
        {
           
            if (lanbamchuot == 0)
            {
                diem1.X = e.X;
                diem1.Y = e.Y;
                lanbamchuot = 1;
            }

            else 
             {
                lanbamchuot =0;
                 if (chedo!=0 && chedo!= 4) landt=0;
                 //vatthe[soluong] = duong;
                 vatthe[soluong] = hv;
                 soluong++;
                 vecapnhat();
             }
           
        }


        private void xulychuotclickkhivedagiac(MouseEventArgs e)
        { }

     

        private void xulychon(MouseEventArgs e)
        {
            Point d;
            d = new Point();
            d.X = e.X; d.Y = e.Y;
 
            int i;
      
            for (i = 0; i < soluong; i++)
            {
                if (vatthe[i].kiemtraduocchon(d) == true)
                {
                   
                     if (vatthe[i].duocchon == false)
                    {
                        vatthe[i].ve();
                         vatthe[i].vechon();
                        vatthe[i].duocchon = true;
                     
                        g.DrawImage(anh, 0, 0, anh.Width, anh.Height);
                    }
                    //  break;
                }
                else
                {
                   if (vatthe[i].duocchon == true)
                    {
                        vatthe[i].bochon();
                        vatthe[i].ve ();
                       // txt1.Text += " "+i.ToString();
                        g.DrawImage(anh, 0, 0, anh.Width, anh.Height);
                    }
                    vatthe[i].duocchon = false;
                }
               
        }
    }

        private void dichuyen(object sender, MouseEventArgs e)
        {
            //txt1.Text = e.X.ToString() + " " + e.Y.ToString();
            switch (chedo)
            {
                case 0: xửlíchuộtdichuyểnkhiởchếđộchọn (e); break;

              case 1: case 2: case 3: case 5: xulychuotdichuyenkhikhongvedagiac(e); break;
               // case 2: xulychuotdichuyenkhiveduongtron(e); break;
               // case 3: xulychuotdichuyenkhivehinhchunhat(e); break;
               // case 5: xulychuotdichuyenkhiveparabol(e); break;
            }
        }
        private void xửlíchuộtdichuyểnkhiởchếđộchọn(MouseEventArgs e)
    {   // txt1.Text += e.Clicks.ToString() ;
            int i;
             Point d=new Point();
             d.X = e.X; d.Y = e.Y;

             if  (e.Button == MouseButtons.Left)
             { 

                 
                 if (lan == 0)
                 {

                     for (i = 0; i < soluong; i++)
                     {
                         if (vatthe[i].kiemtraduocchon(d) == true)
                         {
                             vatthe[i].duocchon = true;
                             co = true;
                             break;
                         }
                     }
                     if (co == true)
                     {
                         dangchon = i;
                         lan = 1;
                         diem11 = d;
                         return;
                     }
                     
                 }
           
                 else
                     if (co == true)
                     {
                         switch(chedobiendoi)
                         {
                             case 0: vatthe[dangchon].tinhtien(diem11, d); diem11 = d; break;
                             case 1: vatthe[dangchon].quay (diem11,d); diem11 = d; break;
                         }
                     //    txt1.Text = d.X.ToString() + " " + d.Y.ToString();
                         //txt1.Text += "**" + diem11.X.ToString() + " " + d.X.ToString()
                        // + " " + diem11.Y.ToString() + " " + d.Y.ToString();
                         g.DrawImage(anh, 0, 0, anh.Width, anh.Height);

                     }

             }
             else { lan = 0; co = false; }
    
        }

        private void xulychuotdichuyenkhikhongvedagiac(MouseEventArgs e)
        {
           // MessageBox.Show("click duong thang");  
            if (lanbamchuot == 1)
            {   diem2.X = e.X; 
                diem2.Y = e.Y;
                if (diem1.X < 0 || diem1.Y < 0 || diem2.X < 0 || diem2.Y < 0
                       || diem1.Y > anh.Height  || diem2.Y > anh.Height)
                    MessageBox.Show("diem1.x:" + diem1.X + " diem2.x:" + diem2.X);
                if (landt==0)
                {
                    switch (chedo)
                    {
                        case 1: hv = new Cline(anh, maunentoancuc, diem1, diem2); break;
                        case 2: hv = new Cduongtron(anh, maunentoancuc, diem1, diem2); break;
                        case 3: hv = new Chinhchunhat(anh, maunentoancuc, diem1, diem2); break;
                        case 5: hv = new Cparabol(anh, maunentoancuc, diem1, diem2); break;
                    }
                    landt = 1;
                }
                else// landt >0
                {
                   // xoa duong cu
                    hv.mauve =maunentoancuc;
                    hv.ve ();
                   // MessageBox.Show("aaa");
                   
                   switch (chedo)
                    {
                        case 1: hv = new Cline(anh, maunentoancuc, diem1, diem2); break;
                        case 2: hv = new Cduongtron(anh, maunentoancuc, diem1, diem2); break;
                        case 3: hv = new Chinhchunhat(anh, maunentoancuc, diem1, diem2); break;
                        case 5: hv = new Cparabol(anh, maunentoancuc, diem1, diem2); break;
                    
                    }
                    //hv = new Cline(anh, maunentoancuc, diem1, diem2); 
                }
                hv.ve ();
                g.DrawImage(anh, 0, 0, anh.Width, anh.Height);
  
            }
        }


        private void PaintPic(object sender, PaintEventArgs e)
        {
            vecapnhat();
        }

        private void vecapnhat()
        {
            for (i = 0; i < soluong; i++)
                vatthe[i].ve();
            g.DrawImage(anh, 0, 0, anh.Width, anh.Height);
           

        }

        private void button2_Click(object sender, EventArgs e)
        {
            /*Process tt = new Process();
            tt.StartInfo.FileName = "c:\\BHome.exe";
            tt.Start();*/
            vecapnhat();

        }


       private void hienthuoctinh(MouseEventArgs e)
       {Point d=new Point();
             d.X = e.X; d.Y = e.Y;
           int i;
           for (i = 0; i < soluong; i++)
               if (vatthe[i].kiemtraduocchon(d) == true)
                   vatthe[i].hienthuoctinh(); 

       }


        private void CheDoChon(object sender, EventArgs e)
        {
            chedo = 0;
        }

        private void CheDoDuongThang(object sender, EventArgs e)
        {
            chedo = 1;
        }
        private void chonduongtron(object sender, EventArgs e)
        {
            chedo = 2;
        }

        private void chonhinhchunhat(object sender, EventArgs e)
        {
            chedo = 3;
        }

        private void chondagiac(object sender, EventArgs e)
        {
            chedo = 4;
        }

        private void chonParabol(object sender, EventArgs e)
        {
            chedo = 5;
        }

        private void chếđộquay(object sender, EventArgs e)
        {
            chedobiendoi = 1;
        }

        private void chếđộđốixứngtheochiềudọc(object sender, EventArgs e)
        {
            chedobiendoi = 2;

        }

        private void chếđộđốixứngtheochiềungang(object sender, EventArgs e)
        {
            chedobiendoi = 3;
        }

        private void chọnchếđộtịnhtiến(object sender, EventArgs e)
        {
            chedobiendoi = 0;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            Cline aa = new Cline(anh, maunentoancuc, 0, 100, 200, 150);
            aa.ve();
            aa.ve();
            for(int i=1; i<10; i++)
            {
                 aa = new Cline(anh, maunentoancuc, 0, i+10, 200, 150);
            //aa.quay1goc(10);
            aa.ve();
            aa.ve();
            }
            g.DrawImage(anh, 0, 0, anh.Width, anh.Height);
        }

    }
}